package com.example.qinghe.service.businesstrip;

import com.alibaba.fastjson.JSON;
import com.example.qinghe.dao.businesstrip.JourneyMapper;
import com.example.qinghe.dao.communication.EnterpriseMapper;
import com.example.qinghe.entity.Common;
import com.example.qinghe.entity.businesstrip.Journey;
import com.example.qinghe.entity.communication.Enterprise;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class JourneyService {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private JourneyMapper dao;
    @Autowired
    private EnterpriseMapper enterpriseMapper;

    //添加报销申请
    public int add(Journey reimburse) {
        return dao.add(reimburse);
    }

    // <!--根据种类：type，创建人：sid，编号：id 查找账单,是否已报销:isreimburse,查找起始点：start，几条数据：limit-->
    public List<Common> sel(Common common) {
        return dao.sel(common);
    }

    //修改
    public int del(Common common) {
        return dao.del(common);
    }
    public int  edit(Common common) {
        return dao.edit(common);
    }

    //添加前检查行程是否有冲突，sid:员工id,1,格式、starttime:开始时间 、endtime:结束时间
    public int  checktime(Common common,String sidlist) {
        String[] s = sidlist.split(",");
        for (int i = 0; i < s.length; i++) {
            common.put("sid",","+s[i]+",");

            if(s[i]!=null&&s[i]!="")
            {
                int checktime = dao.checktime(common);
                System.out.println(JSON.toJSONString(common)+"<<<<"+checktime);
                if(checktime>0)
                return Integer.parseInt(s[i]);
            }
        }
        return 0;
    }

    //根据开始时间starttime和结束时间endtime、sid:员工id,1,格式查看每天的行程个数，
    public  List<Common> getDays(Common common) {
        return dao.getDays(common);
    }

    //根据行程id查看行程
    public Common selById(Common common) {
        return dao.selById(common);
    }
    public  List<Common> sellist(Common common) {
        List<Common> sellist = dao.sellist(common);
        for (int c = 0; c < sellist.size(); c++) {
            System.out.println(JSON.toJSONString(sellist.get(c)));
            Long eid =(Long) sellist.get(c).get("enterprise_id");
            sellist.get(c).put("belong",getBelong(eid));
        }
        return sellist;
    }
    public String getBelong(Long eid) {
        System.out.println("eid"+eid);
        Enterprise detail = enterpriseMapper.detail(eid);
        String belong=detail.getName();
        while (detail.getParent_id()!=1)
        {
            detail = enterpriseMapper.detail(detail.getParent_id());
            belong= detail.getName()+" "+belong;
        }
        return belong;
    }



}
